package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.api.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.api.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.api.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.service.IDestinatonService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.common.query.StrategyCommentQueryObject;
import cn.wolfcode.luowowo.common.query.StrategyDetailQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.templates.StrategyTemplate;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.GetEndDayTime;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Reference
    private IDestinatonService destinatonService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private IUserInfoService userInfoService;
    @RequestMapping("/detail")
    public String detail(Model model,Long id){
        //共享detail过去
        StrategyDetail detail = strategyDetailService.getById(id);
        //查询内容
        StrategyContent strategyContent = strategyDetailService.getContentById(id);
        detail.setStrategyContent(strategyContent);
        //共享到页面
        model.addAttribute("detail",detail);

        //统计阅读量,每进入一次,数量+1
        strategyStatisRedisService.viewIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,id);
        //从redis中获取值,共享到页面
        StrategyStatisVO statisVo = strategyStatisRedisService.getStatisVo(id);
        model.addAttribute("vo",statisVo);
        //每阅读一次,热门增加1分
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,detail.getId(),1);
        return "strategy/detail";

    }
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQueryObject qo){
        //共享吐司
        List<Destination> toasts = destinatonService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        //查询所有的标签tags共享过去
        List<StrategyTag> tags =  strategyTagService.selectAll();
        model.addAttribute("tags",tags);
        //查询当前destId下的所有detail共享过去
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategy/list";
    }
    //攻略评论
    @RequireLogin
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        strategyCommentService.save(comment);

        //评论数量
        strategyStatisRedisService.replyIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,comment.getDetailId());
        //每评论一次,热门增加1
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,comment.getDetailId(),1);
        return new AjaxResult().addData(strategyStatisRedisService.getStatisVo(comment.getDetailId()).getReplynum());
    }
    //攻略评论显示
    @RequestMapping("comment")
    public String comment(Model model,@ModelAttribute("qo") StrategyCommentQueryObject qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }

    //点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    @RequireLogin
    public Object commentThumbUp(String toid,Long fromid){
      boolean ret = strategyCommentService.thumup(toid,fromid);
      return new AjaxResult(ret,"");
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        if(userInfo  == null){
            return new AjaxResult("请先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        //站在攻略的角度
        //boolean ret =  strategyStatisRedisService.favor(StrategyStatisKeyPrefix.STRATEGY_STATIS_FAVOR,sid,userInfo.getId());
        //站在用户的角度
        boolean ret =  strategyStatisRedisService.Userfavor(StrategyStatisKeyPrefix.USER_STATIS_FAVOR,sid,userInfo.getId());
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,sid,ret?1:-1);
        return new AjaxResult(ret,"").addData(strategyStatisRedisService.getStatisVo(sid));
    }

    //顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        if(userInfo  == null){
            return new AjaxResult("请先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        Long time = GetEndDayTime.getEndTime();//获取截止到23:59:59秒还有多少时间(秒)
        StrategyStatisKeyPrefix keyPrefix = new StrategyStatisKeyPrefix("strategy_statis_thumbsup", time);
        //自定义day
        //boolean ret =  strategyStatisRedisService.thumbup(StrategyStatisKeyPrefix.STRATEGY_STATIS_THUMBSUP,sid,userInfo.getId());
        //使用日历类,计算截止到今天还有多少时间

        boolean ret =  strategyStatisRedisService.thumbupTime(keyPrefix,sid,userInfo.getId());
        if(ret){
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,sid,1);

            //如果顶成功添加积分(用来购买道具的)
            userInfoService.addUserCoinById(userInfo.getId(),3000L);
        }
        return new AjaxResult(ret,"").addData(strategyStatisRedisService.getStatisVo(sid));
    }

    //攻略推荐页面
    @RequestMapping("")
    public String index(Model model){
        //查询出所有的推荐排行数据
       List<StrategyStatisVO> all = strategyStatisRedisService.getAllStrategyCommends();
       //国内集合
        ArrayList<StrategyStatisVO> unabroadCds = new ArrayList<>();
        //国外集合
        ArrayList<StrategyStatisVO> abroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : all) {
            if(vo.isIsabroad()){
                if(abroadCds.size() < 10 ){

                    abroadCds.add(vo);
                }
            }else{
                if(unabroadCds.size() < 10){

                    unabroadCds.add(vo);
                }
            }
        }
        //共享到页面
        model.addAttribute("abroadCds",abroadCds);
        model.addAttribute("unabroadCds",unabroadCds);

        //查询所有的攻略,选取前十个,共享过去,不区分国内外
        model.addAttribute("hotCds",all.subList(0,10));

      List<StrategyCommend> list =  strategyCommendService.selectCommendsTop5();
      model.addAttribute("commends",list);

      //主题攻略推荐themeCds
       List<Map<String,Object>> themeCds =  strategySearchService.queryThemeCommends();
       model.addAttribute("themeCds",themeCds);

       //chinas,国内,按省份来分组
      List<StatisVO> chinas = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_UN_ABROAD);
      model.addAttribute("chinas",chinas);
        // abroads 国外,按国家来分组
        List<StatisVO> abroads = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads",abroads);
        //themes,按主题分组
        List<StatisVO> themes = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes",themes);
      return "strategy/index";


    }

    //最受欢迎
    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo") SearchQueryObject qo){
      Page<StrategyTemplate> page =  strategySearchService.query(qo);
      model.addAttribute("page",page);
      return "strategy/searchPageTpl";
    }
}
